package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.adapter.MultiAdapter
import com.example.administrator.kotlin.bean.MultiBean
import com.example.administrator.kotlin.bean.Newslist

class MultiItemActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: MultiAdapter
    private val datas: List<Newslist>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multi_item)
        initView()
//        initData()
    }

    private fun initData(): ArrayList<MultiBean> {
        val list = ArrayList<MultiBean>()
        for (i in 0..10) {
            val spansize = (Math.random() * 3 + 1).toInt()
            val spansize1 = (Math.random() * 3 + 1).toInt()
            val spansize2 = (Math.random() * 3 + 1).toInt()
            list.add(MultiBean(MultiBean.TEXT, spansize, "text 条目占 $spansize /6 份"))
            list.add(MultiBean(MultiBean.IMAGE, spansize1, "text 条目占 $spansize1/6 份"))
            list.add(MultiBean(MultiBean.TEXT_IMAGE, spansize2, "text 条目占 $spansize2/6 份"))
        }
        return list
    }

    private fun initView() {
        recyclerView = findViewById<RecyclerView>(R.id.recyclerview_multi)
        recyclerView.layoutManager = GridLayoutManager(this@MultiItemActivity, 6)
        adapter = MultiAdapter(initData())
        adapter.setSpanSizeLookup { _, position ->
            adapter.data.get(position).spanSize
        }
        recyclerView.adapter = adapter
    }
}
